#!/bin/sh -e
#
# firmware loader agent
#

cd /lib/udev/
. ./hotplug.functions

if [ ! -e /sys/$DEVPATH/loading ]; then
    mesg "/sys/$DEVPATH/ does not exist"
    exit 1
fi

FIRMWARE_DIRS="/lib/firmware/$(uname -r) /lib/firmware /usr/local/lib/firmware /usr/lib/hotplug/firmware"

for DIR in $FIRMWARE_DIRS; do
    [ -e "$DIR/$FIRMWARE" ] || continue
    echo 1 > /sys/$DEVPATH/loading
    cat "$DIR/$FIRMWARE" > /sys/$DEVPATH/data
    echo 0 > /sys/$DEVPATH/loading
    exit 0
done

# the firmware was not found
echo -1 > /sys/$DEVPATH/loading

RUNDIR=$(udevadm info --run)

if [ -d "$RUNDIR" ]; then
    mkdir -p "$RUNDIR/firmware-missing/"
    file=$(echo "$FIRMWARE" | sed -e 's#/#\\x2f#g')
    ln -s -f "$DEVPATH" "$RUNDIR/firmware-missing/$file"
fi

debug_mesg "Cannot find the $FIRMWARE firmware"
exit 1

